<?php
class C_project extends CI_Controller {
	function __construct(){
		parent::__construct();
		checkLogin();
		$this->load->model('m_project');
		$this->load->model('m_document');
		$this->load->helper('input_helper');
		$this->load->model('m_public_function');
	}
	function index(){
		$task = secure_post('task');
		switch($task){
			case 'LIST' :
				$this->_list();
				break;
			case 'INSERT' :
				$this->_insert();
				break;
			case 'UPDATE' :
				$this->_update();
				break;
			case 'DELETE' :
				$this->_delete();
				break;
			default :
				$data["m_cabang"] = $this->m_public_function->m_cabang();
				$this->load->view('v_project',$data);
		}
	}
	
	function _list(){
		$data['page'] = numeric_post("page");
		$data['rows'] = numeric_post("rows");
		$data['project_name'] = nosql_post("project_name");
		$data['project_code'] = nosql_post("project_code");
		$data['project_date'] = nosql_post("project_date");
		$data['project_value'] = nosql_post("project_value");
		echo $this->m_project->_list($data);
	}
	
	function _insert(){
		$data = array(
			'm_cabang_id' => $this->input->post('m_cabang_id'), 
			'project_name' => $this->input->post('project_name'), 
			'project_code' => $this->input->post('project_code'), 
			'project_date' => input_date($this->input->post('project_date')), 
			'project_value' => str_replace(array(",","."),array(".",""), $this->input->post('project_value')), 
			  
			'project_insert_user'	=>$_SESSION[SESSION_USERNAMA],
			'project_insert_date'=> date("Y-m-d h:i:s"));
		$project_id = $this->m_project->_insert($data);
        
 /*       $flows = $this->m_public_function->_flow();
        
        foreach($flows as $flow){
            $data_document = array(
                'document_noflow' => $flow->flow_code, 
                'document_noproyek' => $this->input->post('project_code'), 
                'document_no' => "", 
                't_project_id' => $project_id, 
                'm_flow_id' => $flow->flow_id, 
                  
                'document_insert_user'	=>$_SESSION[SESSION_USERNAMA],
                'document_insert_date'=> date("Y-m-d h:i:s"));
            
            $document_id = $this->m_document->_insert($data_document);
        }
        */
        
		echo 1;
	}
	
	function _update(){
		$project_id = $this->input->post('project_id');
		$data = array(
			'm_cabang_id' => $this->input->post('m_cabang_id'), 
			'project_name' => $this->input->post('project_name'), 
			'project_code' => $this->input->post('project_code'), 
			'project_date' => input_date($this->input->post('project_date')), 
			'project_value' => str_replace(array(",","."),array(".",""), $this->input->post('project_value')), 
			
			'project_update_user'	=>$_SESSION[SESSION_USERNAMA],
			'project_update_date'=> date("Y-m-d h:i:s"));
		
		$this->m_project->_update($data,$project_id);
		echo 1;
 	}
	
	function _delete(){
		$project_id= $this->input->post('project_id');
		$data = array(
			'project_id' => $project_id
		);
		$this->m_project->_delete($data);
		echo 1;
	}
}